package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import cn.edu.tju.core.model.User;
import jakarta.persistence.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "orders")
public class Order extends BaseEntity {
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_id", nullable = false)
    private User customer;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_id", nullable = false)
    private Business business;

    @Column(name = "order_date", nullable = false)
    private LocalDateTime orderDate;

    @Column(name = "order_total", precision = 10, scale = 2, nullable = false)
    private BigDecimal orderTotal;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "address_id", nullable = false)
    private DeliveryAddress deliveryAddress;
    
    @Column(name = "order_state", nullable = false)
    private Integer orderState;
    
    // 添加与订单明细的关联
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<OrderDetailet> orderDetailets = new ArrayList<>();

    // getters and setters
    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    public Business getBusiness() {
        return business;
    }

    public void setBusiness(Business business) {
        this.business = business;
    }

    public LocalDateTime getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(LocalDateTime orderDate) {
        this.orderDate = orderDate;
    }

    public BigDecimal getOrderTotal() {
        return orderTotal;
    }

    public void setOrderTotal(BigDecimal orderTotal) { 
        this.orderTotal = orderTotal;
    }

    public DeliveryAddress getDeliveryAddress() {
        return deliveryAddress;
    }

    public void setDeliveryAddress(DeliveryAddress deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
    }

    public Integer getOrderState() {
        return orderState;
    }

    public void setOrderState(Integer orderState) {
        this.orderState = orderState;
    }
    
    public List<OrderDetailet> getOrderDetailets() {
        return orderDetailets;
    }
    
    public void setOrderDetailets(List<OrderDetailet> orderDetailets) {
        this.orderDetailets = orderDetailets;
    }
    
    // 辅助方法：添加订单明细
    public void addOrderDetailet(OrderDetailet detailet) {
        orderDetailets.add(detailet);
        detailet.setOrder(this);
    }
    
    // 辅助方法：移除订单明细
    public void removeOrderDetailet(OrderDetailet detailet) {
        orderDetailets.remove(detailet);
        detailet.setOrder(null);
    }
    
    // 辅助方法：计算订单总额
    @Transient
    public BigDecimal calculateTotal() {
        BigDecimal total = BigDecimal.ZERO;
        for (OrderDetailet detailet : orderDetailets) {
            if (detailet.getFood() != null && detailet.getQuantity() != null) {
                BigDecimal itemTotal = detailet.getFood().getFoodPrice().multiply(BigDecimal.valueOf(detailet.getQuantity()));
                total = total.add(itemTotal);
            }
        }
        return total;
    }
    
    // 新增方法：获取订单状态（枚举形式）
    @Transient
    public OrderStatus getStatus() {
        if (orderState == null) {
            return null;
        }
        return OrderStatus.fromValue(orderState);
    }
    
    // 新增方法：设置订单状态
    public void setStatus(OrderStatus status) {
        this.orderState = status != null ? status.getValue() : null;
    }
    
    // 新增方法：检查订单是否可以删除
    @Transient
    public boolean canBeDeleted() {
        OrderStatus status = getStatus();
        return status != null && status.canDelete();
    }
    
    // 新增方法：获取订单状态显示文本
    @Transient
    public String getOrderStateDisplay() {
        OrderStatus status = getStatus();
        return status != null ? status.getDisplayName() : "未知状态";
    }
}